Package it.czerwinski.kotlin.util

Contains utility types based on Scala.

Types

Link copied to clipboard
sealed class Either<out L, out R>

Disjoint union. The instance might be either Left or Right.

Link copied to clipboard
data class Failure(val exception: Throwable) : Try<Nothing>
Link copied to clipboard
data class Left<out L>(val value: L) : Either<L, Nothing>
Link copied to clipboard
data class LeftProjection<out L, out R>(val either: Either<L, R>)
Link copied to clipboard
object None : Option<Nothing>

Representation of a non-existent value.

Link copied to clipboard
sealed class Option<out T>

Representation of an optional value. The instance might be either Some value or None.

Link copied to clipboard
data class Right<out R>(val value: R) : Either<Nothing, R>
Link copied to clipboard
data class RightProjection<out L, out R>(val either: Either<L, R>)
Link copied to clipboard
data class Some<T>(val value: T) : Option<T>

Representation of a value of type T.

Link copied to clipboard
data class Success<out T>(val value: T) : Try<T>
Link copied to clipboard
sealed class Try<out T>

Representation of an operation that might successfully return a value or throw an exception.

Functions

Link copied to clipboard
fun <T> T?.asOption(): Option<T>

Returns Some if this is not null or None if this is null.

Link copied to clipboard
operator fun <T> Option<T>.contains(element: T): Boolean

Tests whether the Option contains the given element.

Link copied to clipboard
fun <T> Option<Try<T>>.evert(): Try<Option<T>>

Moves inner Try outside of the outer Option.

fun <T> Try<Option<T>>.evert(): Option<Try<T>>

Moves inner Option outside of the outer Try.

Link copied to clipboard
fun <L, R> Either<L, R?>.filterNotNull(): Either<L, R>?
fun <L, R> RightProjection<L, R?>.filterNotNull(): Either<L, R>?

Returns the same Right if its value is not null. Otherwise returns null.

fun <L, R> LeftProjection<L?, R>.filterNotNull(): Either<L, R>?

Returns the same Left if its value is not null. Otherwise returns null.

fun <T> Try<T?>.filterNotNull(): Try<T>

Returns the same Success if its value is not null. Otherwise returns a Failure.

Link copied to clipboard
fun <L, R> Either<L, R?>.filterNotNullToOption(): Option<Either<L, R>>
fun <L, R> RightProjection<L, R?>.filterNotNullToOption(): Option<Either<L, R>>

Returns Some containing the same Right if its value is not null. Otherwise returns None.

fun <L, R> LeftProjection<L?, R>.filterNotNullToOption(): Option<Either<L, R>>

Returns Some containing the same Left if its value is not null. Otherwise returns None.

Link copied to clipboard
inline fun <L, R> Either<L, R>.filterOrElse(predicate: (R) -> Boolean, zero: () -> L): Either<L, R>
inline fun <L, R> RightProjection<L, R>.filterOrElse(predicate: (R) -> Boolean, zero: () -> L): Either<L, R>

Returns the same Right if the predicate is satisfied for the value, Left(zero) if the predicate is not satisfied for the value, or the same Left if this is Left.

inline fun <L, R> LeftProjection<L, R>.filterOrElse(predicate: (L) -> Boolean, zero: () -> R): Either<L, R>

Returns the same Left if the predicate is satisfied for the value, Right(zero) if the predicate is not satisfied for the value, or the same Right if this is Right.

Link copied to clipboard
inline fun <L, R, T> Either<L, R>.flatMap(transform: (R) -> Either<L, T>): Either<L, T>
inline fun <L, R, T> RightProjection<L, R>.flatMap(transform: (R) -> Either<L, T>): Either<L, T>

Maps value of this Right to a new Either using transform.

inline fun <L, R, T> LeftProjection<L, R>.flatMap(transform: (L) -> Either<T, R>): Either<T, R>

Maps value of this Left to a new Either using transform.

Link copied to clipboard
fun <T> Option<Option<T>>.flatten(): Option<T>

Transforms a nested Option to a not nested Option.

fun <T> Option<Try<T>>.flatten(): Option<T>

Returns Some if this Some contains a Success. Otherwise returns None.

fun <T> Option<Iterable<T>>.flatten(): List<T>

Returns nested List if this is Some. Otherwise returns an empty List.

fun <T> Try<Option<T>>.flatten(): Option<T>

Extracts an Option nested in the Try to a not nested Option.

fun <T> Try<Try<T>>.flatten(): Try<T>

Transforms a nested Try to a not nested Try.

fun <T> Iterable<Option<T>>.flatten(): List<T>

Returns List of values of each Some in this Iterable.

Link copied to clipboard
inline fun <L, R> Either<L, R>.getOrElse(default: () -> R): R
inline fun <L, R> RightProjection<L, R>.getOrElse(default: () -> R): R

Gets value of this Right or default value if this is Left.

inline fun <L, R> LeftProjection<L, R>.getOrElse(default: () -> L): L

Gets value of this Left or default value if this is Right.

inline fun <T> Option<T>.getOrElse(default: () -> T): T

Gets the value of a Some or default value if this is None.

inline fun <T> Try<T>.getOrElse(default: () -> T): T

Gets the value of a Success or default value if this is a Failure.

Link copied to clipboard
fun <L, R> Either<Either<L, R>, R>.joinLeft(): Either<L, R>

Returns this if this is Right. Otherwise returns value of Left.

Link copied to clipboard
fun <L, R> Either<L, Either<L, R>>.joinRight(): Either<L, R>

Returns this if this is Left. Otherwise returns value of Right.

Link copied to clipboard
fun <T> Either<T, T>.merge(): T

Merges Left and Right of the same type to a single value.

Link copied to clipboard
inline fun <T> Option<T>.orElse(default: () -> Option<T>): Option<T>

Returns this Option if this is a Some or default if this is None.

inline fun <T> Try<T>.orElse(default: () -> Try<T>): Try<T>

Returns this Try if this is a Success or default if this is a Failure.

Link copied to clipboard
inline fun <T> Try<T>.recover(rescue: (Throwable) -> T): Try<T>

Returns this Try if this is a Success or a Try created for the rescue operation if this is a Failure.

Link copied to clipboard
inline fun <T> Try<T>.recoverWith(rescue: (Throwable) -> Try<T>): Try<T>

Returns this Try if this is a Success or a Try created by the rescue function if this is a Failure.

Link copied to clipboard
fun <A, B> Option<Pair<A, B>>.unzip(): Pair<Option<A>, Option<B>>

Transforms an Option of a Pair into a Pair of an Option of the first value and an Option of the second value.

fun <A, B, C> Option<Triple<A, B, C>>.unzip(): Triple<Option<A>, Option<B>, Option<C>>

Transforms an Option of a Triple into a Triple of an Option of the first value, an Option of the second value, and an Option of the third value.